package com.admin.kotlintest.activity.setting

import android.view.View
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import cn.bmob.v3.listener.SaveListener
import cn.bmob.v3.listener.UpdateListener
import com.admin.kotlintest.R
import com.admin.kotlintest.adapter.SpinnerTypeAdapter
import com.admin.kotlintest.base.BaseActivity
import com.admin.kotlintest.bean.MyUser
import com.admin.kotlintest.bean.Type
import com.admin.kotlintest.bean.UserType
import com.admin.kotlintest.utils.ImageUtils
import com.socks.library.KLog
import kotlinx.android.synthetic.main.activity_user_setting.*
import kotlinx.android.synthetic.main.layout_title.*
import org.jetbrains.anko.toast

/**
 * Created by 吴昶 on 2018/4/17.
 */
class UserSettingActivity:BaseActivity(){

    private var user:MyUser?=null
    private var spinnerTypeAdapter:SpinnerTypeAdapter?=null
    private var type:Type?=null

    override fun bindLayout(): Int {
        return R.layout.activity_user_setting
    }

    override fun initData() {
        user=intent.getSerializableExtra("user") as MyUser
        user!!.header?:let {
            civ_user_header.setImageResource(R.mipmap.ic_launcher_round)
        }
        user!!.header?.let {
            ImageUtils.GlideLoadImage(context,user!!.header!!.fileUrl,civ_user_header,R.mipmap.ic_launcher_round,R.mipmap.ic_launcher_round)
        }
        tv_user_name.text=user!!.username
        tv_phone_number.text=user!!.mobilePhoneNumber

        spinnerTypeAdapter= SpinnerTypeAdapter(context,R.layout.layout_spinner_type_item,null)
        sp_type.adapter=spinnerTypeAdapter
        getTypes()

        tv_title_right.text="保存"
        linear_title_right.visibility= View.VISIBLE
        tv_title_right.visibility= View.VISIBLE

        var query=BmobQuery<UserType>()
        query.addWhereEqualTo("user",user)
        query.include("type")
        query.findObjects(object :FindListener<UserType>(){
            override fun done(p0: MutableList<UserType>?, p1: BmobException?) {
                p1?:let {
                    p0?.let {
                        KLog.d(p0.size.toString())
                        if(p0.size>0){
                            type=p0[0].type
                            KLog.d(type.toString())
                            tv_user_type.text=p0[0].type!!.name
                        }else{
                            KLog.d("========")
                        }
                    }
                }
            }})
    }

    override fun initEvent() {

        iv_title_left.setOnClickListener {
            finish()
        }

        tv_title_right.setOnClickListener{
            var query=BmobQuery<UserType>()
            query.addWhereEqualTo("user",user)
            query.findObjects(object :FindListener<UserType>(){
                override fun done(p0: MutableList<UserType>?, p1: BmobException?) {
                    p1?:let {
                        p0?.let {
                            if (p0.size >= 1) {
                                var objectId = p0[0].objectId
                                var userType = UserType(user, spinnerTypeAdapter!!.getItem(sp_type.selectedItemPosition))
                                userType.update(objectId, object : UpdateListener() {
                                    override fun done(p0: BmobException?) {
                                        p0 ?: let {
                                            toast("修改成功")
                                        }
                                        p0 ?.let {
                                            KLog.d(p0.message.toString())
                                            toast(p0.message.toString())
                                        }
                                    }
                                })
                            }else{
                                saveType()
                            }
                        }
                        p0 ?: let {
                            saveType()
                        }
                    }

                    p1?.let {
                        KLog.d(p1.message.toString())
                        saveType()
                    }
                }
            })

        }
    }

    /**
     * 获取用户所有的类型列表
     */
    fun getTypes(){
        var query= BmobQuery<Type>()
        query.findObjects(object : FindListener<Type>(){
            override fun done(p0: MutableList<Type>?, p1: BmobException?) {
                if(p1==null) {
                    p0?.let {
                        spinnerTypeAdapter!!.upData(p0)
                    }
                }
            }
        })


    }

    fun saveType(){
        var userType = UserType(user, spinnerTypeAdapter!!.getItem(sp_type.selectedItemPosition))
        userType.save(object : SaveListener<String>() {
            override fun done(p0: String?, p1: BmobException?) {
                p1 ?: let {
                    KLog.d(p0)
                    toast("修改成功")
                    finish()
                }
                p1?.let {
                    toast("修改失败" + p1.message.toString())
                }
            }
        })
    }

}